package com.yuncheng.spcyApi.spcy;

import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.alog.utils.StringEmptyUtils;
import com.yuncheng.spcyApi.constant.jcy.JcyLxjhConstant;
import com.yuncheng.spcyApi.entity.SpcyPxJbxx;
import com.yuncheng.spcyApi.entity.SpcySqsxWjjl;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.service.ISpcyPxJbxxService;
import com.yuncheng.spcyApi.spcyService.ZdyUploadFileService;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;

/**
 * @author hua
 */
@Api(tags = "检查员继续教育培训管理")
@RestController
@RequestMapping("/api/spzx-spcy/jxjypx")
public class JcyJxjypxController {

    @Resource
    private ISpcyPxJbxxService spcyPxJbxxService;

    @Resource
    private ZdyUploadFileService zdyUploadFileService;


    @Log(title = "继续教育-分页查询")
    @ApiOperation(value = "继续教育-分页查询")
    @GetMapping(value = "pageList")
    public HttpResult pageList(SpcyPxJbxx query, String gjz, PageVo pageVo, OrderItemVo orderItemVo){
        query.setFpxlx(JcyLxjhConstant.JCYLX_PXLX_JXJYPX);
        return HttpResult.ok(spcyPxJbxxService.pageList(query, gjz, pageVo,orderItemVo));
    }

    @Log(title = "继续教育-添加")
    @ApiOperation(value = "继续教育-添加")
    @PostMapping(value = "addLxjh")
    public HttpResult addLxjh(@RequestBody SpcyPxJbxx bean){
        bean.setFpxlx(JcyLxjhConstant.JCYLX_PXLX_JXJYPX);
        SpcyPxJbxx SpcyPxJbxx = spcyPxJbxxService.addLxjh(bean);

        return HttpResultVo.HttpResultSave(SpcyPxJbxx, "添加成功");
    }

    @Log(title = "继续教育-更新")
    @ApiOperation(value = "继续教育-更新")
    @PostMapping(value = "updateLxjh")
    public HttpResult updateLxjh(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        spcyPxJbxxService.updateJxjy(buffer);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "继续教育-删除")
    @ApiOperation(value = "继续教育-删除")
    @DeleteMapping(value = "deleteLxjh")
    public HttpResult deleteLxjh(@RequestParam(value = "ids",defaultValue = "")String ids){
        if (org.apache.commons.lang3.StringUtils.isEmpty(ids)){
            throw new BusinessException("参数为空!");
        }

        List<String> listByIds = StringPatternUtils.getListByIds(ids);

        spcyPxJbxxService.deleteJxjy(ids);

        // 清空附件
        if (CollectionUtil.isNotEmpty(listByIds)) {
            for (String id : listByIds) {
                List<SpcySqsxWjjl> wjjlList = zdyUploadFileService.queryWjjlByPid(id);
                if (CollectionUtil.isNotEmpty(wjjlList)) {
                    for (SpcySqsxWjjl wjjl : wjjlList) {
                        zdyUploadFileService.deleteFileRecords(wjjl);
                        zdyUploadFileService.deleteFileByUrl(wjjl.getFcflj());
                    }
                }
            }
        }
        return HttpResult.ok("删除成功");
    }

    @Log(title = "继续教育-根据id查看")
    @ApiOperation(value = "继续教育-根据id查看")
    @GetMapping(value = "queryById")
    public HttpResult queryById(@RequestParam(value = "id",defaultValue = "")String id){
        if (StringEmptyUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }
        SpcyPxJbxx jbxx = spcyPxJbxxService.getById(id);
        return HttpResult.ok(jbxx);
    }
}
